package com.ruoyi.common.core.utils;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class NumberUtil {

    private static final String[] CN_NUM = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};

    public static String int2Chinese(Integer intNum) {
        StringBuffer sb = new StringBuffer();
        int count = 0;
        while (intNum > 0) {
            sb.insert(0, CN_NUM[intNum % 10]);
            intNum = intNum / 10;
            count++;
        }
        return sb.toString();
    }

    public static String number2Chinese(String number) {
        StringBuilder stringBuffer = new StringBuilder();
        String[] split = number.split("");
        for (int i = split.length - 1; i >= 0; i--) {
            if (split[i].equals("0")) {
                stringBuffer.insert(0, CN_NUM[0]);
            }
            if (split[i].equals("1")) {
                stringBuffer.insert(0, CN_NUM[1]);
            }
            if (split[i].equals("2")) {
                stringBuffer.insert(0, CN_NUM[2]);
            }
            if (split[i].equals("3")) {
                stringBuffer.insert(0, CN_NUM[3]);
            }
            if (split[i].equals("4")) {
                stringBuffer.insert(0, CN_NUM[4]);
            }
            if (split[i].equals("5")) {
                stringBuffer.insert(0, CN_NUM[5]);
            }
            if (split[i].equals("6")) {
                stringBuffer.insert(0, CN_NUM[6]);
            }
            if (split[i].equals("7")) {
                stringBuffer.insert(0, CN_NUM[7]);
            }
            if (split[i].equals("8")) {
                stringBuffer.insert(0, CN_NUM[8]);
            }
            if (split[i].equals("9")) {
                stringBuffer.insert(0, CN_NUM[9]);
            }
        }

        return stringBuffer.toString();
    }

    public static String string2Chinese(String string) {
        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if (Character.isDigit(aChar)) {
                boolean digit = Character.isDigit(aChar);
                if (digit) {
                    chars[i] = NumberUtil.number2Chinese(String.valueOf(aChar)).charAt(0);
                }
            }
        }
        return new String(chars);
    }

    public static boolean withinInterval(int compareNum, int min, int max) {
        return Math.max(min, compareNum) == Math.min(compareNum, max);
    }

    public static String withinInterval(Double compareNum, String rangeList) {
        String result = "";
        String[] splitArray = rangeList.split(";");
        for (String split : splitArray) {
            String[] tmp = split.split(",");
            List<String> rangeNums = Arrays.stream(tmp).map(str -> str.replaceAll("[(|)|\\[|\\]]", "").trim()).collect(Collectors.toList());
            if (withinInterval(compareNum.intValue(), Integer.parseInt(rangeNums.get(0)), Integer.parseInt(rangeNums.get(1)))) {
                result = split;
            }
        }
        return result;
    }

    public static int rangeAvg(String range){
        String[] tmp = range.split(",");
        List<String> rangeNums = Arrays.stream(tmp).map(str -> str.replaceAll("[(|)|\\[|\\]]", "").trim()).collect(Collectors.toList());
        return (BigDecimal.valueOf(Long.parseLong(rangeNums.get(0))).add(BigDecimal.valueOf(Long.parseLong(rangeNums.get(1))))).divide(BigDecimal.valueOf(2)).intValue();
    }

}
